<article class="module width_full">
    <header><h3 class="tabs_involved">DANH MỤC TÁC GIẢ</h3>
        <ul class="tabs">
            <li><a href="#tab1">Tất cả</a></li>
        </ul>
    </header>
    <div class="tab_container"  style="overflow-y:scroll; height:200px;width: 100%;">
        <div id="tab1" class="tab_content">
            <table class="table demo default  footable tablesorter table-hover" cellspacing="0" > 
                <thead> 
                    <tr> 
                        <th data-type="numeric" data-sort-initial="true" >STT</th> 
                        <th  >Mã tác giả</th> 
                        <th >Tên tác giả </th>                        
<!--                        <th >Tiểu sử </th>-->
                        <th data-sort-ignore="true">Hành động</th> 
                    </tr> 
                </thead> 
                <tbody id="category"> 
                    <?php
                    $i = 0;

                    foreach ($tacgias as $value) {

                        $i+=1;
                        echo "<tr id='row" . $value->MATG . "'>";
                        echo '<td>' . $i . '</td>';
                        echo '<td class="td_matg">' . $value->MATG . '</td>';
                        echo '<td class="td_tentg">' . $value->TENTG . '</td>';
//                        echo '<td class="td_tieusu">' . $value->TIEUSU . '</td>';
                        echo "<td><a class='edit-category' href='" . $value->MATG . "'><input type='image' src='" . base_url() . "public/admin/images/icn_edit.png' title='Chỉnh sửa thể loại sách '></a>
					<a class='delete-cate' href='" . $value->MATG . "'><input type='image' src='" . base_url() . "public/admin/images/icn_trash.png'title='thể loại sách'></a>
					<a class='add-cate'><input type='image' src='" . base_url() . "public/admin/images/icn_add_user.png' title='Thêm loại sách mới'></a></td>";
                        echo '</tr>';
                    }
                    ?>
                </tbody> 
            </table>
        </div><!-- end of #tab1 -->






    </div><!-- end of .tab_container -->

</article><!-- end of content manager article -->	
<!-- Modal add -->
<div class="modal fade" id="myModal-add" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">Thêm mới tác giả</h4>
            </div>
            <div class="modal-body">
                <form id="form-add" class="form-horizontal" role="form">
                    <div class="form-group">
                        <label for="inputEmail3" class="col-sm-2 control-label">Tên tác giả </label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control tennxb"  id="tentg-add" required="true" name="tentg" placeholder="Tên tác giả">
                        </div>
                    </div>

                    <div class="form-group">
                        <div class=" col-sm-12">
                            <?php echo form_textarea(array('name' => 'tieusu', 'id' => 'tieusu-add', 'class' => "ckeditor diachi", 'placeholder' => 'Thông tin về nhà xuất bản')); ?>
                        </div>
                    </div>

                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button type="submit" form="form-add" id="btn-add" class="btn btn-primary">Lưu</button>
            </div>
        </div>
    </div>
</div>
<!--End Modal add -->
<!-- Modal add -->
<div class="modal fade" id="myModal-edit" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">Thêm mới tác giả</h4>
            </div>
            <div class="modal-body">
                <form id="form-edit" class="form-horizontal" role="form">
                    <div class="form-group">
                        <label for="inputEmail3" class="col-sm-2 control-label">Tên tác giả </label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control tennxb"  id="tentg-edit" required="true" name="tentg" placeholder="Tên tác giả">
                        </div>
                    </div>

                    <div class="form-group">
                        <div class=" col-sm-12">
                            <?php echo form_textarea(array('name' => 'tieusu', 'id' => 'tieusu-edit', 'class' => "ckeditor diachi", 'placeholder' => 'Thông tin về nhà xuất bản')); ?>
                        </div>
                    </div>

                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button type="submit" form="form-edit" id="btn-edit" class="btn btn-primary">Lưu</button>
            </div>
        </div>
    </div>
</div>
<script>
    $(document).ready(function() {
        //matg 
        var Globalmatg;
        //xuất hiện giao diện thêm mới
        $('.add-cate').click(function(e) {
            e.preventDefault();
            $("#myModal-add").modal();

        });
        $("#form-add").submit(function(e) {
            e.preventDefault();
            var tentg = $("#tentg-add").val();
            var tieusu = CKEDITOR.instances['tieusu-add'].getData();
            $.ajax({
                type: "POST",
                url: globalurl + 'index.php/admin/tacgia_add',
                data: {
                    tentg: tentg,
                    tieusu: tieusu
                },
                success: function(data) {
                    // $('#category').append(data);
                    alert(data);
                    $('#myModal-add').modal('hide');

                }, error: function(xhr, ajaxOptions, thrownError) {
                    alert(xhr.status);
                    alert(thrownError);
                }
            });
        });
        $('.edit-category').click(function(e) {
            e.preventDefault();
            Globalmatg = $(this).attr("href");
            $.ajax({
                type: "POST",
                url: globalurl + 'index.php/admin/getTG',
                data: {
                    matg: Globalmatg
                },
                success: function(data) {                 
                    $("#tentg-edit").val(data.tentg);
                    CKEDITOR.instances["tieusu-edit"].setData(data.tieusu);
                    $("#myModal-edit").modal();
                }, dataType: 'json'
            });
        });


        $('#form-edit').submit(function(e) {
            e.preventDefault();
            var tentg = $("#tentg-edit").val();
            var tieusu = CKEDITOR.instances['tieusu-edit'].getData();
            $.ajax({
                type: "POST",
                url: globalurl + 'index.php/admin/tacgia_upd',
                data: {
                    matg: Globalmatg,
                    tentg: tentg,
                    tieusu: tieusu
                },
                success: function(data) {                
                    $('#row' + Globalmatg).find(".td_tentg").text(data.tentg);
                    $("#myModal-edit").modal('hide');
                }, error: function(xhr, ajaxOptions, thrownError) {
                    alert(xhr.status);
                    alert(thrownError);
                },dataType:'json'
            });


        });

        $('.delete-cate').click(function(e) {
            e.preventDefault();
            var matg = $(this).attr("href");
            
            if (confirm('Bạn có muốn xóa tác giả này ?')) {
                $.ajax({
                    type: "POST",
                    url: globalurl + 'index.php/admin/tacgia_del',
                    data: {
                        matg: matg
                    },
                    success: function(data) {
                        alert(data.rs);
                        if(data.rs==true)
                        $('#row' + matg).remove();
                    else alert("Lỗi");
                    }, error: function(xhr, ajaxOptions, thrownError) {
                        alert(xhr.status);
                        alert(thrownError);
                    },dataType:'json'
                });
            }

        });

        $('.add-cate').click(function(e) {
            e.preventDefault();

            $.ajax({
                type: "POST",
                url: globalurl + 'index.php/admin/view_addtheloai',
                data: {
                },
                success: function(data) {
                    $('#add_form').html(data);
                    $('#add_form').toggle("slow");

                }
            });


        });

        $('#add_form').submit(function(e) {
            e.preventDefault();
            alert('d');
            var tentl = $('#catename').val();
            $.ajax({
                type: "POST",
                url: globalurl + 'index.php/admin/addnewtheloai',
                data: {
                    tentl: tentl
                },
                success: function(data) {
                    // $('#category').append(data);
                    alert(data);
                    $('#add_form').toggle("slow");

                }, error: function(xhr, ajaxOptions, thrownError) {
                    alert(xhr.status);
                    alert(thrownError);
                }
            });


        });


    });

</script>
